package com.guigu.serviceorder.controller;


import com.alibaba.fastjson.JSON;
import com.guigu.common_util.pojo.utils.Result;
import com.guigu.serviceorder.entity.TbOrder;
import com.guigu.serviceorder.entity.TbReturnOrder;
import com.guigu.serviceorder.entity.TbReturnOrderItem;
import com.guigu.serviceorder.service.ITbReturnOrderItemService;
import com.guigu.serviceorder.service.ITbReturnOrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.ws.rs.GET;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author jobob
 * @since 2020-07-08
 */
@RestController
@RequestMapping("/returnOrder")
public class TbReturnOrderController {

    @Autowired
    private ITbReturnOrderService iTbReturnOrderService;

    @Autowired
    private ITbReturnOrderItemService iTbReturnOrderItemService;

    // 退货分页条件查询
    @PostMapping("/findPage1")
    public Result findPage1(@RequestBody Map<String, Object> searchMap, Integer page, Integer size) {
        return iTbReturnOrderService.findPage1(searchMap, page, size);
    }
    // 退款分页条件查询
    @PostMapping("/findPage2")
    public Result findPage2(@RequestBody Map<String, Object> searchMap, Integer page, Integer size) {
        return iTbReturnOrderService.findPage2(searchMap, page, size);
    }

    @PostMapping("/add")
    public Result insert(@RequestBody TbReturnOrder tbReturnOrder){
        if (iTbReturnOrderService.save(tbReturnOrder)==true)
            return Result.ok();
        return  Result.error();
    }

    @GetMapping("/delete")
    public Result delete(Long id){
        if(iTbReturnOrderService.removeById(id))
            return Result.ok();
        return Result.error();

    }
    @PostMapping("/update")
    public Result update(@RequestBody TbReturnOrder tbReturnOrder){
        if (iTbReturnOrderService.updateById(tbReturnOrder))
            return Result.ok();
        return Result.error();
    }

    @GetMapping("/findById")
    public Result findById(Long id) {
        TbReturnOrder tbReturnOrder = iTbReturnOrderService.getById(id);
        if (tbReturnOrder == null) {
            return Result.error();
        }
        return Result.ok().data("tbReturnOrder", tbReturnOrder);
    }

    /**
     * 同意退款
     * @param
     * @param
     * @return
     */
    @GetMapping("/argeeRefund")
    public Result argeeRefund(String id,Integer returnMoney,Integer adminId){
        return iTbReturnOrderService.agreeRefund(id,returnMoney,adminId);
    }
    @GetMapping("/rejectRefund")
    public Result rejectRefund(String id,String remark,Integer adminId){
        return iTbReturnOrderService.rejectRefund(id,remark,adminId);
    }
    @GetMapping("/argeeRefundGoods")
    public Result argeeRefundGoods(String id,Integer adminId){
        return iTbReturnOrderService.agreeRefundGoods(id,adminId);
    }
    @GetMapping("/rejectRefundGoods")
    public Result rejectRefundGoods(String id,Integer adminId,String remark){
        return iTbReturnOrderService.rejectRefundGoods(id,remark,adminId);
    }
    @GetMapping("/findOrderItemByOrderId")
    public Result findOrderItemByOrderId(String id) {
        return this.iTbReturnOrderService.findOrderItemByOrderId(id);
    }
    @GetMapping("/findReturnOrderItemByReturnOrderId")
    public Result findReturnOrderItemByReturnOrderId(String id) {
        return this.iTbReturnOrderService.findReturnOrderItemByReturnOrderId(id);
    }
    @PostMapping("insertAddress")
    public Result insertAddress(@RequestBody  Map map){
        System.out.println(map);
        return Result.ok();
    }

    @PostMapping(value = "/deletereturnOrder",produces = "application/json")
    public Result deletereturnOrder(@RequestBody  Map map){
        System.out.println(map);
        String map1 =(String) map.get("map");
        List<TbReturnOrder> returnOrders = JSON.parseArray(map1, TbReturnOrder.class);
        return this.iTbReturnOrderService.deletereturnOrder(returnOrders);
    }

}

